import fetcher from "../apis/Fetcher";
import {NumberIdAndName} from "../types/BasicModels";
import {QueryGoodsResponse} from "../apis/GoodsApi";
import {Page, Pageable} from "../utils/Page";
import {OffsetDateTime} from "@js-joda/core";

export function createMarketCheckInventoryRecord(id: number) {
    return fetcher.post<number>(`markets/checkInventoryRecords/${id}/orders`)
}

export function getMarketCheckInventoryRecordDetail(id: number) {
    return fetcher.get<MarketCheckInventoryRecordDetail>(`markets/checkInventoryRecords/${id}`, {
        includeItems: true
    })
}

export function getMarketCheckInventoryRecordDetailWithoutItems(id: number) {
    return fetcher.get<MarketCheckInventoryRecordDetail>(`markets/checkInventoryRecords/${id}`)
}

export interface MarketCheckInventoryRecordDetail {
    id: number
    name:string
    createdDateTime: OffsetDateTime
    createdBy: NumberIdAndName
    items: Array<MarketCheckInventoryRecordItem>
    submitDateTime?: OffsetDateTime
    market:NumberIdAndName
}

export interface MarketCheckInventoryRecordItem extends QueryGoodsResponse {
    amountInOrder: number
}

export interface AddMarketCheckInventoryRecordItemRequest {
    amount: number
    goodsId: string
}

export function addMarketCheckInventoryRecordItem(id: number, addMarketCheckInventoryRecordItemRequest: AddMarketCheckInventoryRecordItemRequest) {
    return fetcher.post(`markets/checkInventoryRecords/${id}/items`, {}, addMarketCheckInventoryRecordItemRequest)
}

export function deleteMarketCheckInventoryRecordItem(orderId: number, goodsId: number) {
    return fetcher.delete(`markets/checkInventoryRecords/${orderId}/items/${goodsId}`)
}

export interface UpdateMarketCheckInventoryRecordItemAmountRequest {
    amount: number
}

export function updateMarketCheckInventoryRecordItemAmount(orderId: number, goodsId: number, updateMarketCheckInventoryRecordItemAmountRequest: UpdateMarketCheckInventoryRecordItemAmountRequest) {
    return fetcher.patch(`markets/checkInventoryRecords/${orderId}/items/${goodsId}/amount`, {}, updateMarketCheckInventoryRecordItemAmountRequest)
}

export interface QueryMarketCheckInventoryRecordRequest {
    startDate?: string
    endDate?: string
    employeeIds: number[]
    submitted?:boolean
    marketIds:number[]
}

export function queryMarketCheckInventoryRecords(queryMarketCheckInventoryRecordRequest: QueryMarketCheckInventoryRecordRequest, pageable: Pageable) {
    return fetcher.get<Page<QueryMarketCheckInventoryRecordResponse>>("markets/checkInventoryRecords", {...queryMarketCheckInventoryRecordRequest, ...pageable})
}

export interface QueryMarketCheckInventoryRecordResponse {
    id: number
    createdBy: NumberIdAndName
    createdDateTime: OffsetDateTime
    goodsKindAmount: number
    name:string
    submittedDateTime?: OffsetDateTime
    market:NumberIdAndName
}

export function getAllMarketCheckInventoryRecordCreatedEmployees() {
    return fetcher.get<Array<NumberIdAndName>>("markets/checkInventoryRecords/employees", {projection: "summary"})
}

export function submitMarketCheckInventoryRecord(id: number) {
    return fetcher.patch<number>(`markets/checkInventoryRecords/${id}/submit`)
}

export function convertToDeliveryOrder(id: number) {
    return fetcher.post<number>(`markets/checkInventoryRecords/${id}/delivery`)
}

export function getAllMarketCheckInventoryRecordMarkets() {
    return fetcher.get<Array<NumberIdAndName>>("markets/checkInventoryRecords/markets", {projection: "summary"})
}

export function getCheckInventoryRecordCreatedEmployeesByMarketId(marketId:number) {
    return fetcher.get<Array<NumberIdAndName>>(`markets/${marketId}/checkInventoryRecords/employees`, {projection: "summary"})
}